import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Test;
import sziit.znk.utils.importothers.FileDownload;
import test.DownloadFileFromJD;

import java.io.*;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;


public class Application {

    @Test
    public void downloadFromBD() throws Exception{
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        FileDownload fileDownload = new FileDownload();
        String url = "https://pan.baidu.com/api/streaming?path=%2F%E5%AD%A6%E4%B9%A0%2F%E5%8D%87%E5%AD%A6%2F%E5%8D%87%E6%9C%AC%2F00152%20%E7%BB%84%E7%BB%87%E8%A1%8C%E4%B8%BA%E5%AD%A6%2F%E8%A7%86%E9%A2%91%2F%E3%80%90%E7%BB%84%E7%BB%87%E8%A1%8C%E4%B8%BA%E5%AD%A6%E3%80%91%E5%B9%BF%E4%B8%9C%E4%BA%BA%E5%8A%9B%E6%9C%AC_%E7%B2%BE%E8%AE%B25_-%E5%9B%9E%E6%94%BE.mp4&app_id=250528&clienttype=0&type=M3U8_AUTO_480&vip=0&adToken=3i4UriUBcQig8tGV8THqyqOXtC8LBo7I1wIkQdBjPcOq1nBOuL%2BAq1kJreEWAHNz4a5f9R%2BLre01gcPSyEK0sAOHfTEGmOnTG2kRyWaN2AEw6nto%2BN8nybXdrzgkPFOjxKR7J3Uv2d0XVmO5%2FnuS56RNOmetn01m5LV7b3p%2FPCY%3D";
        fileDownload.saveFile(url,"video","GET","精讲5.mp4");
        System.out.println("ok");
    }



    @Test
    public void getFromDF() throws Exception{
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        HtmlPage page = webClient.getPage("https://resource.stockstar.com/DataCenter/StockData/GetSimpleGuide.aspx?ob=5");
//        List<HtmlElement> title = page.getByXPath("/html/body/div[9]/div[2]/table/tbody/tr[1]");
//        Iterator<DomElement> trs = title.get(0).getChildElements().iterator();
//        while (trs.hasNext()) {
//            System.out.println(trs.next().asText());
//        }//横向布局
        List<HtmlElement> tbody = page.getByXPath("/html/body/div[9]/div[2]/table/tbody");
//        List<HtmlElement> one = tbody.get(0).getByXPath("tr[1]");
//        Iterator<DomElement> oneIterator = one.get(0).getChildElements().iterator();
//        while (oneIterator.hasNext()) {
//            System.out.println(oneIterator.next().asText());
//        }//成功了title
        int childCount = tbody.get(0).getChildElementCount();
        for (int i = 1; i <= childCount; i++) {//i=1包括了title;i=2不包括title
            List<HtmlElement> twoAfter = tbody.get(0).getByXPath("tr[" + i + "]");
            Iterator<DomElement> iterator = twoAfter.get(0).getChildElements().iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next().asText());
            }
        }
    }

    @Test
    public void downloadFileFromJD() throws Exception {
        DownloadFileFromJD fileFromJD = new DownloadFileFromJD();
        fileFromJD.downloadFileFromJD("https://search.jd.com/Search?keyword=%E5%8D%8E%E4%B8%BA&enc=utf-8&wq=%E5%8D%8E%E4%B8%BA&pvid=7311ab0cc86f4fb3a961113fd7245b1a");
    }

    @Test
    public void test() throws Exception {
//        HttpUtil util = new HttpUtil();
//        String s = util.createHttpConnection("http://www.weather.com.cn/", "get");

        // 实例化Web客户端、①模拟 Chrome 浏览器 ✔ 、②使用代理IP ✔
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
//        WebClient webClient = new WebClient(BrowserVersion.CHROME, "118.114.77.47", 8080);
        webClient.getOptions().setCssEnabled(false); // 取消 CSS 支持 ✔
        webClient.getOptions().setJavaScriptEnabled(false); // 取消 JavaScript支持 ✔
        try {
//            HtmlPage page = webClient.getPage("http://www.weather.com.cn/"); // 解析获取页面
//            HtmlPage page = webClient.getPage("https://www.csdn.net/"); // 解析获取页面
            HtmlPage page = webClient.getPage("http://www.weather.com.cn/weather15d/101280601.shtml"); // 解析获取页面

            /**
             * Xpath:级联选择 ✔
             * ① //：从匹配选择的当前节点选择文档中的节点，而不考虑它们的位置
             * ② h3：匹配<h3>标签
             * ③ [@class='company_name']：属性名为class的值为company_name
             * ④ a：匹配<a>标签
             */
//            List<HtmlElement> spanList = page.getByXPath("//div[@class='nav_addr']//ul//li//a[@href='http://www.weather.com.cn/forecast/']");
//            List<HtmlElement> spanList = page.getByXPath("//h3[@class='company_name']/a");
            List<HtmlElement> spanList = page.getByXPath("//div[@id='15d']/ul/li");

            for (int i = 0; i < spanList.size(); i++) {
                //asText ==> innerHTML ✔
                System.out.println(i + 1 + "、" + spanList.get(i).asText());
            }

        } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            webClient.close(); // 关闭客户端，释放内存
        }

    }

    @Test
    public void test_02() throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false); // 取消 CSS 支持 ✔
        webClient.getOptions().setJavaScriptEnabled(false); // 取消 JavaScript支持 ✔
        try {
            HtmlPage page = webClient.getPage("https://www.bilibili.com/"); // 解析获取页面
            List<HtmlElement> html = page.getByXPath("//body");


//            List<HtmlElement> spanList = page.getByXPath("//*[@id=\"reportFirst1\"]/div[2]/div[1]/div[1]");
//
//            for (int i = 0; i < spanList.size(); i++) {
//                //asText ==> innerHTML ✔
//                System.out.println(i + 1 + "、" + spanList.get(i).asText());
//            }

        } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            webClient.close(); // 关闭客户端，释放内存
        }

    }

}
